/*
 * 
 */
package org.demo.filemanager.widget.filesystem;

import java.awt.event.MouseEvent;
import java.io.File;
import java.util.ArrayList;

import org.boxlayout.gui.BoxLayoutEngine;
import org.boxlayout.gui.MouseCustomable;
import org.boxlayout.gui.VisualElement;
import org.boxlayout.gui.text.TextManager;
import org.boxlayout.gui.widget.AbstractOnDemandTreeBrowsableWidget;
import org.boxlayout.gui.widget.TreeBrowsable;

/**
 * TODO: Edit class comment
 * 
 * @author dodekane@gmail.com
 */
public class FileWidget extends AbstractOnDemandTreeBrowsableWidget {

	FileWidget me = this;
	
	VisualElement imageElement;


	String path;

	VisualElement childLayout = null;

	VisualElement childContainer = null;

	float childContainerScale = 1.0f;

	Boolean expanded = false;

	Boolean highlighted = false;

	//DirectoryFileWidget parentFileWidget;

	
	MouseCustomable onMouseIn = new MouseCustomable() {

		public void onEvent(MouseEvent event) {
			// System.out.println("mousein:"+nameContainer.getPickId());
			highlighted = true;
			nameContainer
					.setTextSize(TextManager.TEXT_SCALE_FACTOR_DEFAULT * 1.1f);
			ble.reLayout();
		}
	};

	MouseCustomable onMouseOut = new MouseCustomable() {
		public void onEvent(MouseEvent event) {
			// System.out.println("mouseout:"+nameContainer.getPickId());
			highlighted = false;
			nameContainer.setTextSize(TextManager.TEXT_SCALE_FACTOR_DEFAULT);
			ble.reLayout();
		}
	};
	
	public FileWidget(BoxLayoutEngine ble, String path) {
		super(ble);
		name = (new File(path)).getName();
		this.path = path;
		this.childs = new ArrayList<TreeBrowsable>();
		//visual =  initVisual();
	}

	
	public TreeBrowsable initVisual() {
		super.initVisual();
		nameContainer.setMouseInListener(onMouseIn).setMouseOutListener(onMouseOut)
		.setAlpha(1.0f).setRounded(true)
		;		
		return this;
	}
	
	
	/*VisualElement init() {
		File file = new File(path);
		isDirectory = file.isDirectory();
		color = (isDirectory ? ColorEnum.YELLOW : ColorEnum.GRAY);
		nameContainer = ble.createElement()
				// Name
				.setColor(color).setColor2(ColorEnum.LIGHTGRAY).setPadding(
						0.05f).setText(file.getName()).setMouseIn(onMouseIn)
				.setMouseOut(onMouseOut);

		visual = ble.createElement().setLayout(LayoutFactory.getInstance().getVerticalLayout()).setPadding(
				0.00f).setColor(ColorEnum.WHITE);
		visual.add(nameContainer);

		if (file.isDirectory()) {
			nameContainer.setOnClick(folderBehaviour);
		}
		if (path.indexOf("jpg") != -1) {
			nameContainer.setOnClick(imageBehaviour);
		}
		return visual;
	}*/
	
	
/*
	private void retrieveChilds() {
		File file = new File(path);
		isDirectory = file.isDirectory();
		if (isDirectory) {
			File[] childFiles = file.listFiles();
			fileWidgetChilds.clear();
			childContainer = ble.createElement()
					// Childs
					.setLayout(LayoutFactory.getInstance().getVerticalLayout()).setPadding(padding)
					.setColor(ColorEnum.WHITE);

			for (int i = 0; i < childFiles.length; i++) {
				File childFile = childFiles[i];
				FileWidget newChildFileWidget = new FileWidget(ble, childFile
						.getAbsolutePath(), selector);
				fileWidgetChilds.add(newChildFileWidget);
				newChildFileWidget.setParentFileWidget(this);
				childContainer.add(newChildFileWidget.getVisual());
			}

			VisualElement newChildLayout = ble.createElement().setLayout(
					LayoutFactory.getInstance().getHorizontalLayout()).setPadding(padding).setColor(
					ColorEnum.WHITE).add(// blank
					ble.createElement().setPadding(padding).setWidth(0.1f)
							.setHeight(0.0f).setColor(ColorEnum.WHITE)).add(
					childContainer);

			if (childLayout == null) {
				visual.add(newChildLayout);
			} else {
				visual.replace(childLayout, newChildLayout);
			}
			childLayout = newChildLayout;
		}
	}
	*/

	/*void setParentFileWidget(DirectoryFileWidget file) {
		parentFileWidget = file;
	}

	public DirectoryFileWidget getParent() {
		return parentFileWidget;
	}*/

	public FileWidget processFinalize() {
		visual.prepareLayout();
		visual.processLayout();
		return this;
	}

	public VisualElement getVisual() {
		return visual;
	}

	/*public void setChildExpended(Boolean expend) {
		if (isDirectory) {
			if (!expanded && expend) {
				retrieveChilds();
				childLayout.setVisible(true);
				ble.reLayout();
			} else if (expanded && !expend) {
				childLayout.setVisible(false);
				ble.reLayout();
			}
			expanded = expend;
		}
	}*/

	

	public FileWidget setRoot(Boolean root) {
		this.root = root;
		this.visual.setRoot(true);
		return this;
	}
	
	public boolean isRoot() {
		return root;
	}
	

}
